#R-code based on Riscam
#ISCAM : Integrated 
#Daved Martell,
#Fisheries Centre, UBC, Quantitative Fisheries Group
read.admb <- function (ifile) 
{
    ret = read.fit(ifile)
    fn = paste(ifile, ".rep", sep = "")
    A = read.rep(fn)
    A$fit = ret
    pfn = paste(ifile, ".psv", sep = "")
    if (file.exists(pfn)) 
        A$post.samp = read.psv(pfn)
    return(A)
}


read.fit <- function(ifile) 
{
    ret <- list()
    parfile <- as.numeric(scan(paste(ifile, ".par", sep = ""), 
        what = "", n = 16, quiet = TRUE)[c(6, 11, 16)])
    ret$nopar <- as.integer(parfile[1])
    ret$nlogl <- parfile[2]
    ret$maxgrad <- parfile[3]
    file <- paste(ifile, ".cor", sep = "")
    lin <- readLines(file)
    ret$npar <- length(lin) - 2
    ret$logDetHess <- as.numeric(strsplit(lin[1], "=")[[1]][2])
    sublin <- lapply(strsplit(lin[1:ret$npar + 2], " "), function(x) x[x != 
        ""])
    ret$names <- unlist(lapply(sublin, function(x) x[2]))
    ret$est <- as.numeric(unlist(lapply(sublin, function(x) x[3])))
    ret$std <- as.numeric(unlist(lapply(sublin, function(x) x[4])))
    ret$cor <- matrix(NA, ret$npar, ret$npar)
    corvec <- unlist(sapply(1:length(sublin), function(i) sublin[[i]][5:(4 + 
        i)]))
    ret$cor[upper.tri(ret$cor, diag = TRUE)] <- as.numeric(corvec)
    ret$cor[lower.tri(ret$cor)] <- t(ret$cor)[lower.tri(ret$cor)]
    ret$cov <- ret$cor * (ret$std %o% ret$std)
    return(ret)
}


#Lee el report file#Then the ’A’ object contains a list structure#with all the elements in the report file.#In the REPORT SECTION of the AMDB template use#the following format to out put objects :# report <<"object \n"<< object <<endl;## The part in quotations becomes the list name.# Created By Steven Martell

read.rep <- function(fn) 
{
    options(warn = -1)
    ifile = scan(fn, what = "character", flush = TRUE, blank.lines.skip = FALSE, 
        quiet = TRUE)
    idx = sapply(as.double(ifile), is.na)
    vnam = ifile[idx]
    nv = length(vnam)
    A = list()
    ir = 0
    for (i in 1:nv) {
        ir = match(vnam[i], ifile)
        if (i != nv) 
            irr = match(vnam[i + 1], ifile)
        else irr = length(ifile) + 1
        dum = NA
        if (irr - ir == 2) 
            dum = as.double(scan(fn, skip = ir, nlines = 1, quiet = TRUE, 
                what = ""))
        if (irr - ir > 2) 
            dum = as.matrix(read.table(fn, skip = ir, nrow = irr - 
                ir - 1, fill = TRUE))
        if (is.numeric(dum)) {
            A[[vnam[i]]] = dum
        }
    }
    options(warn = 0)
    return(A)
}


read.psv <- function(fn, nsamples = 10000) 
{
    filen <- file(fn, "rb")
    nopar <- readBin(filen, what = integer(), n = 1)
    mcmc <- readBin(filen, what = numeric(), n = nopar * nsamples)
    mcmc <- matrix(mcmc, byrow = TRUE, ncol = nopar)
    close(filen)
    return(mcmc)
}
